<% if false # the license inside this if block assertains to this file -%>
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
<% end -%>
<%= compile('templates/license.erb') -%>

<%= lines(autogen_notice :ruby) -%>

<%
  requires = generate_requires(object.all_user_properties)
  requires << 'puppet'
  unless object.exports.nil?
    requires << 'google/object_store'
  end
-%>
<%= lines(emit_requires(requires)) -%>

<% Google::LOGGER.info "Generating #{object.name}: #{object.out_name}" -%>
Puppet::Type.newtype(:<%= object.out_name -%>) do
<%= format_description(object, 2, '@doc =') %>

  autorequire(:gauth_credential) do
    credential = self[:credential]
    raise "#{ref}: required property 'credential' is missing" if credential.nil?
    [credential]
  end

<%
  unless object.parameters.nil?
    object.parameters.each do |param|
      if param.class <= Api::Type::ResourceRef
        Google::LOGGER.info \
          "Generating autorequire #{object.name}.#{param.name}: #{param.type}"
-%>
  autorequire(:<%= param.out_type -%>) do
    reference = self[:<%= param.out_name -%>]
<% if param.required -%>
<%=
  lines(format(
    [
      ["raise \"\#{ref} required property '#{param.out_name}' is missing\"",
       'if reference.nil?'].join(' '),
      [
        "raise \"\#{ref} required property '#{param.out_name}' is missing\" \\",
        indent('if reference.nil?', 2)
      ]
    ], 4
  ))
-%>
    reference.autorequires
<% else # param.required -%>
    reference.autorequires unless reference.nil?
<% end # param.required -%>
  end

<%
      end
    end
  end
-%>
<% unless object.virtual -%>
  ensurable

<% end -%>
  newparam :credential do
    desc <<-DESC
      A gauth_credential name to be used to authenticate with Google Cloud
      Platform.
    DESC
  end
<% if object.all_user_properties.select { |o| o.name == 'project' }.empty? -%>

  newparam(:project) do
    desc 'A Google Cloud Platform project to manage.'
  end
<% end -%>
<%
  # TODO(nelsonjr): Support multiple namevar. If there are 2+ defined we turn
  # multi name var off.
  identities = if object.__identity.nil? || object.__identity.size != 1
                 []
               else
                 object.__identity
               end
  if identities.empty?
-%>

  newparam(:name, namevar: true) do
    # TODO(nelsona): Make this description to match the key of the object.
    desc 'The name of the <%= object.name -%>.'
  end
<% end -%>
<% unless object.parameters.nil? -%>
<% object.parameters.each do |p| -%>

<% Google::LOGGER.info "Generating param #{object.name}.#{p.name}:#{p.type}" -%>
<%=
  namevar = identities.include?(p.name) ? 'namevar: true' : nil

  format([
    ["newparam(:#{p.out_name}", namevar,
     "parent: #{p.property_type}) do"].compact.join(', '),
    (indent_list([
       "newparam(:#{p.out_name}, #{namevar}",
       # + 12 = newparam(: + ,
       indent("parent: #{p.property_type}) do", p.out_name.length + 12)
     ], 0) unless namevar.nil?),
    indent_list([
      "newparam(:#{p.out_name}",
      (indent(namevar, 9) unless namevar.nil?),
      indent("parent: #{p.property_type}) do", 9)
    ].compact, 0),
  ].compact, 2) %>
<%= format_description(p, 4, 'desc') %>
<%= property_body(p) -%>
<% unless p.validation.nil? -%>
    validate do |value|
<%= lines(indent(p.validation, 6)) -%>
    end
<% end -%>
  end
<% end -%>
<% end -%>
<% object.properties.each do |p|
      Google::LOGGER.info "Generating #{object.name}.#{p.name}: #{p.type}"
-%>

<%=
  new_property_len = 'newproperty('.length
  format([
    ["newproperty(:#{p.out_name}, parent: #{p.property_type}) do"],
    [
      "newproperty(:#{p.out_name},",
      indent("parent: #{p.property_type}) do", new_property_len)
    ],
  ], 2)
%>
<%= format_description(p, 4, 'desc', p.output ? "(output only)" : "") %>
<%= property_body(p) -%>
  end
<% end -%>
<% unless object.exports.nil? -%>

  # Returns all properties that a provider can export to other resources
  def exports
    provider.exports
  end
<% end -%>
end
